package com.gxj.study.shopdemo.fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toolbar;

import com.gxj.study.shopdemo.CreateOrderActivity;
import com.gxj.study.shopdemo.R;
import com.gxj.study.shopdemo.adapter.CartAdapter;
import com.gxj.study.shopdemo.adapter.decoration.DividerItemDecortion;
import com.gxj.study.shopdemo.bean.ShoppingCart;
import com.gxj.study.shopdemo.http.OkHttpHelper;
import com.gxj.study.shopdemo.test.SwipeRefresh;
import com.gxj.study.shopdemo.utils.CartProvider;
import com.gxj.study.shopdemo.widget.CnToolBar;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.List;


public class CartFragment extends BaseFragment implements View.OnClickListener {
    private static final int ACTION_EDIT = 1;
    private static final int ACTION_CAMPALTE = 2;
    private static final String TAG = "CartFragment";

    @ViewInject(R.id.recyclerView)
    private RecyclerView mRecyclerView;
    @ViewInject(R.id.checkbox_all)
    private CheckBox mCheckBox;
    @ViewInject(R.id.txt_total)
    private TextView mTextTotal;
    @ViewInject(R.id.btn_order)
    private Button mBtnOrder;
    @ViewInject(R.id.btn_del)
    private Button mBtnDel;
    @ViewInject(R.id.toolbar)
    private CnToolBar mToolBar;

    private CartAdapter mAdapeter;
    private CartProvider cartProvider;

    private OkHttpHelper httpHelper = OkHttpHelper.getmInstance();

    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cart, container, false);
        return view;
    }

    @Override
    public void init() {
        cartProvider = new CartProvider(getActivity());
        changeToolBar();
        showData();
    }

    @OnClick(R.id.btn_del)
    public void delCart(View view) {
        mAdapeter.delCart();
    }

    @OnClick(R.id.btn_order)
    public void toOrder(View view) {
        Intent intent = new Intent(getActivity(), CreateOrderActivity.class);
        startActivity(intent, true);
    }

    private void showData() {
        List<ShoppingCart> carts = cartProvider.getAll();

        mAdapeter = new CartAdapter(getActivity(), carts, mCheckBox, mTextTotal);

        mRecyclerView.setAdapter(mAdapeter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(new DividerItemDecortion(getActivity(), DividerItemDecortion.VERTICAL_LIST));
    }

    public void refData() {
        mAdapeter.clearData();
        List<ShoppingCart> carts = cartProvider.getAll();
        mAdapeter.addData(carts);
        mAdapeter.showTotalPrice();
    }

    public void changeToolBar() {
        mToolBar.hideSearchView();
        mToolBar.showTextTitle();
        mToolBar.setTitle(R.string.cart);
        mToolBar.getmRightButton().setVisibility(View.VISIBLE);
        mToolBar.setRightButtonText("编辑");
        mToolBar.getmRightButton().setOnClickListener(this);
        mToolBar.getmRightButton().setTag(ACTION_EDIT);
    }

    private void showDelControl() {
        mToolBar.getmRightButton().setText("完成");
        mTextTotal.setVisibility(View.GONE);
        mBtnOrder.setVisibility(View.GONE);
        mBtnDel.setVisibility(View.VISIBLE);
        mToolBar.getmRightButton().setTag(ACTION_CAMPALTE);
        mAdapeter.checkAll_None(false);
        mCheckBox.setChecked(false);
    }

    private void hideDelControl() {
        mTextTotal.setVisibility(View.VISIBLE);
        mBtnOrder.setVisibility(View.VISIBLE);
        mBtnDel.setVisibility(View.GONE);
        mToolBar.setRightButtonText("编辑");
        mToolBar.getmRightButton().setTag(ACTION_EDIT);
        mAdapeter.checkAll_None(true);
        mAdapeter.showTotalPrice();
        mCheckBox.setChecked(true);
    }

    @Override
    public void onClick(View view) {
        int action = (int) view.getTag();
        if (ACTION_EDIT == action) {
            showDelControl();
        } else if (ACTION_CAMPALTE == action) {
            hideDelControl();
        }
    }
}
